package top.xiaoqiannb.shapelessdao.procedures;

import top.xiaoqiannb.shapelessdao.world.inventory.QimenluopanMenu;
import top.xiaoqiannb.shapelessdao.network.ShapelessdaoModVariables;
import top.xiaoqiannb.shapelessdao.init.ShapelessdaoModMobEffects;

import net.minecraftforge.network.NetworkHooks;

import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.MenuProvider;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.network.chat.Component;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.core.BlockPos;

import io.netty.buffer.Unpooled;

public class QimenlaunchProcedure {
	public static void execute(LevelAccessor world, double x, double y, double z, Entity entity) {
		if (entity == null)
			return;
		if (!(entity instanceof LivingEntity _livEnt0 && _livEnt0.hasEffect(ShapelessdaoModMobEffects.COOLDOWN.get()))) {
			if (((entity.getCapability(ShapelessdaoModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ShapelessdaoModVariables.PlayerVariables())).Faith).equals("Dao")) {
				if (((entity.getCapability(ShapelessdaoModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ShapelessdaoModVariables.PlayerVariables())).Sect).equals("QimenI")) {
					if (entity instanceof LivingEntity _livEnt1 && _livEnt1.hasEffect(ShapelessdaoModMobEffects.QIMEN.get())) {
						FDirQimenLunchProcedure.execute(world, entity);
					}
				}
				if (((entity.getCapability(ShapelessdaoModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ShapelessdaoModVariables.PlayerVariables())).Sect).equals("QimenII")) {
					if (entity instanceof LivingEntity _livEnt2 && _livEnt2.hasEffect(ShapelessdaoModMobEffects.QIMEN.get())) {
						if (entity instanceof ServerPlayer _ent) {
							BlockPos _bpos = BlockPos.containing(x, y, z);
							NetworkHooks.openScreen((ServerPlayer) _ent, new MenuProvider() {
								@Override
								public Component getDisplayName() {
									return Component.literal("Qimenluopan");
								}

								@Override
								public AbstractContainerMenu createMenu(int id, Inventory inventory, Player player) {
									return new QimenluopanMenu(id, inventory, new FriendlyByteBuf(Unpooled.buffer()).writeBlockPos(_bpos));
								}
							}, _bpos);
						}
					}
				}
			}
		}
	}
}
